package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.ProLinkBusinessService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.link.business.pojo.dto.LinkBusinessListDTO;
import com.xbongbong.pro.link.business.pojo.dto.LinkBusinessUpdateDTO;
import com.xbongbong.pro.link.business.pojo.vo.LinkBusinessListVO;
import com.xbongbong.pro.link.business.pojo.vo.LinkBusinessUpdateVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/10/30 14:40
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/linkBusiness")
public class ProLinkBusinessController {

    @Resource
    private ProLinkBusinessService proLinkBusinessService;

    /**
     *
     * http://yapi.xbongbong.com/project/87/interface/api/13538
     * @param linkBusinessListDTO
     * @param br
     * @return
     */

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid LinkBusinessListDTO linkBusinessListDTO, BindingResult br) {
        XbbResponse<LinkBusinessListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkBusinessListVO linkBusinessListVO = proLinkBusinessService.list(linkBusinessListDTO);
                response = new XbbResponse<>(linkBusinessListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * http://yapi.xbongbong.com/project/87/interface/api/13543
     * @param linkBusinessUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid LinkBusinessUpdateDTO linkBusinessUpdateDTO, BindingResult br) {
        XbbResponse<LinkBusinessUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkBusinessUpdateVO linkBusinessUpdateVO = proLinkBusinessService.update(linkBusinessUpdateDTO);
                response = new XbbResponse<>(linkBusinessUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
